package com.higgins.lawapp.client.settings.panels;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.higgins.lawapp.client.listeners.DataSourceDTOListBoxListener;
import com.higgins.lawapp.client.settings.components.DataSourceComponent;
import com.higgins.lawapp.client.settings.components.DataSourceElementFields;
import com.higgins.lawapp.client.settings.components.DataSourceFieldFields;
import com.higgins.lawapp.client.settings.components.DataSourceLabelFields;
import com.higgins.lawapp.client.settings.dataModel.DataSourceDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceLevelDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceObjectDTO;
import com.higgins.lawapp.client.settings.model.DataSourceModel;

public class SettingsInternalTabs extends TabPanel	implements DataSourceDTOListBoxListener, ClickHandler {
	private DataSourceModel dataSourceModel = new DataSourceModel();
	private List<SettingsInternalTabsPanel> settingsInternalTabsPanels = new ArrayList<SettingsInternalTabsPanel>();
	private int dataSourceSelectedIndex;
	
	public SettingsInternalTabs()	{
		setHeight("1200px");
		setWidth("800px");
	}

	// DataSource has been selected -> Load a tab for each
	@Override
	public void setDataSourceDTOSelected(DataSourceDTO dataSourceDTO, int dataSourceSelectedIndex) {
		dataSourceModel.setDataSourceDTOSelected(dataSourceDTO);
		if (dataSourceDTO!=null)	{
			this.dataSourceSelectedIndex = dataSourceSelectedIndex;
			List<DataSourceLevelDTO> dataSourceLevelDTOs = dataSourceDTO.getDataSourceLevelDTOs();
			if (dataSourceLevelDTOs.size() > 0)	{
				Iterator<DataSourceLevelDTO> iterator = dataSourceLevelDTOs.iterator();
				int tabNumber = 0;
				clear();
				settingsInternalTabsPanels.clear();
				while (iterator.hasNext())	{
					DataSourceLevelDTO dataSourceLevelDTO = iterator.next();
					// Create Tab
					createSettingsInternalTabsPanel("New Level", "", "", dataSourceLevelDTO, tabNumber);
					tabNumber++;
				}
			}	
		}
		else	{
			// Create Empty Tab
			clear();
			settingsInternalTabsPanels.clear();
			createSettingsInternalTabsPanel("New Level", "", "", null, 0);
		}
		selectTab(0);
	}

	public void createSettingsInternalTabsPanel(String newTabName, String url, String urlExampleField, DataSourceLevelDTO dataSourceLevelDTO, int tabNumber)	{
		LayoutPanel layoutPanel = new LayoutPanel();
		SettingsInternalTabsPanel settingsInternalTabsPanel = new SettingsInternalTabsPanel(this, url, urlExampleField, dataSourceModel, dataSourceLevelDTO, tabNumber);
		layoutPanel.add(settingsInternalTabsPanel);
		layoutPanel.setHeight("600px");
		layoutPanel.setWidth("800px");
		if (dataSourceLevelDTO == null)
			add(layoutPanel, newTabName);
		else
			add(layoutPanel, dataSourceLevelDTO.getDataSourceName());
		settingsInternalTabsPanels.add(settingsInternalTabsPanel);
	}

	@Override
	public void onClick(ClickEvent event) {
		saveDataSource();
	}
	private void saveDataSource()	{		// Save Button
		refreshDataSourceDTOFields();
		dataSourceModel.updateDataSourceDTO();
	}
	private void refreshDataSourceDTOFields()		{
		DataSourceDTO dataSourceDTOSelected = dataSourceModel.getDataSourceDTOSelected();
		
		dataSourceDTOSelected = populateDataSourceDTO(dataSourceDTOSelected);
		
		dataSourceModel.setDataSourceDTOSelected(dataSourceDTOSelected);
	}

	
	public DataSourceDTO populateDataSourceDTO(DataSourceDTO dataSourceDTOSelected)	{
		List<DataSourceLevelDTO> dataSourceLevelDTOs = dataSourceDTOSelected.getDataSourceLevelDTOs();
		int dataSourceLevelIndex = 0;
		if (settingsInternalTabsPanels.size() > 0)	{
			Iterator<SettingsInternalTabsPanel> tabIterator = settingsInternalTabsPanels.iterator();
			// For Each Tab
			while(tabIterator.hasNext())	{
				SettingsInternalTabsPanel settingsInternalTabsPanel = tabIterator.next();
				dataSourceDTOSelected = populateDataSourceLevelDTO(settingsInternalTabsPanel, dataSourceLevelDTOs, dataSourceLevelIndex, dataSourceDTOSelected);
				dataSourceLevelIndex++;
			}
		}		
		return dataSourceDTOSelected;
	}
	

	
	public DataSourceDTO populateDataSourceLevelDTO(SettingsInternalTabsPanel settingsInternalTabsPanel, List<DataSourceLevelDTO> dataSourceLevelDTOs, int dataSourceLevelIndex, DataSourceDTO dataSourceDTOSelected )	{
		DataSourceLevelDTO dataSourceLevelDTO;
		dataSourceLevelDTO = dataSourceDTOSelected.getDataSourceLevelDTO(dataSourceLevelIndex);
		
		settingsInternalTabsPanel.updateDataSourceLevelDTO(dataSourceLevelDTO);
		
		if (dataSourceLevelIndex==0)
			dataSourceDTOSelected.setDataSourceName(settingsInternalTabsPanel.getDataSourceName());
		if (settingsInternalTabsPanel.getDataSourceExecution())		// trigger for parent execution
			dataSourceDTOSelected.setDataSourceExecute(true);

		populateDataSourceItems(dataSourceLevelDTO, settingsInternalTabsPanel);
		return dataSourceDTOSelected;
	}
	
	public void populateDataSourceItems(DataSourceLevelDTO dataSourceLevelDTO, SettingsInternalTabsPanel settingsInternalTabsPanel)	{
		List<DataSourceObjectDTO> dataSourceObjectDTOs = dataSourceLevelDTO.getDataSourceObjectDTOs();
		int dataSourceObjectDTOIndex = 0;
		
		List<DataSourceComponent> dataSourceComponents = settingsInternalTabsPanel.getDataSourceFieldWidgets();
		Iterator<DataSourceComponent> dataSourceComponentIterator = dataSourceComponents.iterator();
		while(dataSourceComponentIterator.hasNext())	{
			DataSourceComponent dataSourceComponent = dataSourceComponentIterator.next();
			DataSourceObjectDTO dataSourceObjectDTO = dataSourceLevelDTO.getDataSourceObjectDTO(dataSourceObjectDTOIndex);
			populateDataSourceItem(dataSourceObjectDTO, dataSourceComponent);
			dataSourceObjectDTOIndex++;
		}
	}

	private void populateDataSourceItem(DataSourceObjectDTO dataSourceObjectDTO, DataSourceComponent dataSourceComponent)	{
		String type = dataSourceComponent.getComponentType();
		if (type.equals("Label"))	{
			DataSourceLabelFields dataSourceLabelFields = (DataSourceLabelFields) dataSourceComponent;
			// Do the DTO update
			dataSourceObjectDTO.setLabel(dataSourceLabelFields.getLabel());
			dataSourceObjectDTO.setNumber(dataSourceLabelFields.getNumber());
			dataSourceObjectDTO.setDataSourceType("Label");
		}	else if (type.equals("Element"))		{
			DataSourceElementFields dataSourceElementFields = (DataSourceElementFields) dataSourceComponent;
			// Do the DTO update
			dataSourceObjectDTO.setStart(dataSourceElementFields.getStart());
			dataSourceObjectDTO.setEnd(dataSourceElementFields.getEnd());
			dataSourceObjectDTO.setDataSourceType("Element");
		}	else if	(type.equals("Field"))		{
			DataSourceFieldFields dataSourceFieldFields = (DataSourceFieldFields) dataSourceComponent;
			// Do the DTO update
			dataSourceObjectDTO.setStart(dataSourceFieldFields.getStart());
			dataSourceObjectDTO.setEnd(dataSourceFieldFields.getEnd());
			dataSourceObjectDTO.setFieldName(dataSourceFieldFields.getFieldName());
			dataSourceObjectDTO.setDataSourceType("Field");
			dataSourceObjectDTO.setURL(dataSourceFieldFields.isURL());
			dataSourceObjectDTO.setConcat(dataSourceFieldFields.isConcat());
		}
		
	}
}
